2 Problem: 11282 - Mixing invitations
3 Author: Andrés Mejía-Posada
4 (http://blogaritmo.factorcomun.org)
6 Algoritm: Combinatorics formula
31 long long choose
[N
+1][N
+1], d
[N
+1]; //d[i] = Number of derangements if i elements.
34 /* Binomial coefficients */
35 for (int i
=0; i
<=N
; ++i
) choose
[i
][0] = choose
[i
][i
] = 1;
36 for (int i
=1; i
<=N
; ++i
)
37 for (int j
=1; j
<i
; ++j
)
38 choose
[i
][j
] = choose
[i
-1][j
-1] + choose
[i
-1][j
];
42 for (int i
=2; i
<=N
; ++i
) d
[i
] = (i
-1)*(d
[i
-2] + d
[i
-1]);
45 while (cin
>> n
>> m
){
47 for (int k
=0; k
<=m
; ++k
) ans
+= choose
[n
][k
] * d
[n
-k
];
54 Explanation: Let f(n, k) = Number of permutations of n elements
55 such that exactly k of them are in their correct position. For
56 example, f(3, 1) = 3 which are {1,3,2}, {3,2,1} and {2,1,3}.
57 Then the answer is sum for every 0 <= k <= m of f(n, k).
59 Let D(n) = Numbers of derangenents of n elements. Note that for
60 every n we have f(n, 0) = D(n). And in general,
62 f(n, k) = choose(n, k) * D(n - k)
64 The idea behind this is that we "nail" k elements from the whole
65 sequence of n elements in choose(n, k) ways and for each possi-
66 bility we disorder completely the missing n - k elements in
69 D(i) is computed using a recurrent function, although could also
70 be found using inclusion-exclusion principle.